<template>
  <div>
    <el-form :model="userForm" label-width="80px" :rules="userFormRules" ref="userForm">

      <el-form-item label="用户名称" prop="username">
        <el-input v-model="userForm.username" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="别名" prop="name">
        <el-input v-model="userForm.name" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="用户类型" prop="utype">
        <el-select v-model="userForm.utype" placeholder="请选择用户类型">
          <el-option
            v-for="item in uTypeList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio-group v-model="userForm.sex">
          <el-radio class="radio" label="1">男生</el-radio>
          <el-radio class="radio" label=0>女生</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="userForm.email" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="userForm.phone" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker type="datetime" placeholder="创建时间" v-model="userForm.createTime"></el-date-picker>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="go_back">返回</el-button>
      <el-button type="primary" @click.native="editSubmit" :loading="addLoading">提交</el-button>
    </div>
  </div>
</template>
<script>
    //导入cmsApi中的所有接口
    import * as userApi from '../api/user'

    export default {
        data() {
            return {
                uTypeList: [
                    {
                        value: '101001', label: '学生'
                    },
                    {
                        value: '101002', label: '教学管理员'
                    },
                    {
                        value: '101003', label: '系统管理员'
                    }],
                //页面id
                userId: '',
                addLoading: false,//加载效果标记
                //新增界面数据
                userForm: {
                    username: '',
                    name: '',
                    utype: '',
                    birthday: '',
                    sex: '',
                    email: '',
                    phone: '',
                    createTime: new Date()
                },
                //表单校验规则
                userFormRules: {
                    username: [
                        {required: true, message: '请选择站点', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            //返回主页面调用的方法
            go_back() {
                //设置路由
                this.$router.push({
                    path: '/system/user/list',
                    //设置路由携带的参数为当前路由中的参数
                    query: {
                        page: this.$route.query.page,
                    }
                })
            },
            editSubmit() {
                this.$refs.userForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            userApi.user_edit(this.userId, this.userForm).then((res) => {
                                console.log(res);
                                if (res.success) {
                                    this.addLoading = false;
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                    //返回主页面
                                    this.go_back();
                                } else {
                                    this.addLoading = false;
                                    if (res.message) {
                                        this.$message.error(res.message);
                                    } else {
                                        this.$message.error('提交失败');
                                    }
                                }
                            });
                        });
                    }
                });
            },
        },
        //钩子方法，页面创建渲染之前调用
        created: function () {
            //从url中获取pageId,params指的是从url中获取指定参数
            this.userId = this.$route.params.userId;
            console.log(this.userId);
            //根据主键查询页面信息
            userApi.user_get(this.userId).then((res) => {
                console.log(res);
                if (res) {
                    //填充表单数据
                    this.userForm = res;
                }
            });
        },
        //钩子方法，在页面渲染之后调用
        mounted: function () {

        }
    }
</script>
<style>

</style>
